#pragma once
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>
#include "def.h"
#include "IceCastHttp.h"
#include "LoginForm.h"
#include "StreamControlWidget.h"
#include "StreamPreviewWidget.h"
#include "StreamDetailWidget.h"
#include "StreamServerWidget.h"
#include "StreamAudioWidget.h"
#include "StreamVideoWidget.h"
#include "StreamConnection.h"
#include "StreamEncoder.h"
#include "MediaSettingForm.h"
#include "StreamSessionWidget.h"

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow(QWidget *parent = 0);
	~MainWindow();

	// show login form
	int showLogin();

	virtual void setVisible(bool visible);

private:
	// controls
	LoginForm *m_frmLogin;
	MediaSettingForm *m_frmSettings;
	StreamControlWidget *m_wdgControlWidget;
	StreamPreviewWidget *m_wdgPreviewWidget;
	StreamDetailWidget *m_wdgDetailWidget;
	StreamServerWidget *m_wdgServerWidget;
	StreamAudioWidget *m_wdgAudioWidget;
	StreamVideoWidget *m_wdgVideoWidget;
	StreamSessionWidget *m_wdgSessionWidget;

	QTimer *m_timer;
	QLabel *m_lblStatus;
	QLabel *m_lblStatus2;
	QLabel *m_lblStatus3;
	QLabel *m_lblTitle;

	QVBoxLayout *m_layMainLayout;
	QWidget *m_wdgMainWidget;
	QMutex m_lockProcessData;

	// functions
	void createInterface();

	// Load and Save settings
	void loadSettings();
	void saveSettings();

	// settings
	int m_audioQuality;
	int m_audioChannels;
	int m_audioSample;
	int m_videoQuality;
	int m_videoFrameRates;
	int m_videoWidth;
	int m_videoHeight;
	int m_videoBitRate;
	int m_nKeyFrameShift;
	int m_bDropFrame;
	int m_bFastMode;
	int m_bAutoBitRate;

	// current status
	int m_nBandwidth;
	int m_nCurFrame[3];
	int m_nCurTime;

	// icecast connection
	IceCastHttp *m_lpIceCastHttp;

	// stream encoder
	StreamEncoder *m_lpStreamEncoder;
	StreamConnection *m_lpStreamConnection;

	int m_bPauseStreaming;

private slots:
	void onStart();
	void onStop();
	void onPause();
	void onResume();
	void onLogin(const char *url, const char *user, const char *pass);
	void onCancel();
	void onUpdate();
	void onCaptureAudio(const TYPE_SAMPLE_IN **data, const int size, const int channels, const int sampleRate);
	void onCaptureVideo(const int width, const int height, const unsigned char *data);
	void onProcessOggPage(const unsigned char* header, const int header_len,const unsigned char* data, const int size);
	void onProcessVideoHeaders(PacketList *packets);
	void onProcessVideoPackets(PacketList *packets);
	void onShowSettings();
	void onSaveSettings(const int audioQuality, const int audioChannels, const int audioSample, const int videoQuality, const int videoFrameRates, const int videoResolution, const int videoBitRate, const int autoBR, const int keyframeshift, const int dropframe, const int fastmode);
	void onTimer();
	void onCurrentSlotChanged(const IceCastSlot*);
	void onVideoSourceChanged(const int index);
	void onVideoPlayback(const bool bPlayback);
	void onVideoPaused(const bool bPaused);

#if DEBUG
	void onLog(const char *msg);
private:
	QTextEdit *m_txtLog;
#endif

protected:
	virtual void closeEvent(QCloseEvent *);
};

#endif // MAINWINDOW_H
